<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Distribute;
use App\Admin\Actions\Grid\ViewTask;
use App\Admin\Repositories\TodoTask;
use App\Admin\Tools\Grid\QuickCreateButton;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Markdown;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Support\Facades\Auth;

class TodoTaskController extends \App\Admin\Controllers\AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TodoTask(['name']), function (Grid $grid) {

            if (!$this->isAdmin()){
                $grid->model()->where('creator_uid', Auth::guard('admin')->id());
            }

            unset($GLOBALS['todo_current_status']);
            $grid->model()->orderBy('status')->orderBy('todo_id', 'desc');
            $grid->column('todo_id', 'TodoID')->sortable();
            $grid->column('task_name');
            $grid->column('name.name', '创建者');
            $grid->status('是否已派发')->using(\App\Models\TodoTask::$todo_status_options)->label(\App\Models\TodoTask::$todo_status_style);
            $grid->column('created_at')->sortable();
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('status', '是否已派发')->radio(\App\Models\TodoTask::$todo_status_options);
            });

            $modal = Modal::make()
                ->lg()
                ->title('快速创建Todo任务')
                ->body(\App\Admin\Forms\QuickCreateTodoForm::make())
                ->button(new QuickCreateButton());
            $grid->tools($modal);

            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->disableRowSelector();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $row = $actions->row;
                if ($row->status == \App\Models\TodoTask::TODO_STATUS_READY){
                    $actions->append(new Distribute());
                }else{
                    $actions->append(new ViewTask());
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TodoTask(['name']), function (Show $show) {
            $show->field('todo_id', 'TodoID');
            $show->field('task_name', '任务名称');

            $show->html(function (){
                $header = '<div class="show-field form-group row"><div class="col-sm-2 control-label"><span>任务描述</span></span></div><div class="col-sm-8"><div class="box box-solid box-default no-margin box-show"><div class="box-body" style="max-height:500px;overflow:auto"> ';
                $content = Markdown::make($this->task_desc);
                $footer = '</div></div></div></div>';
                return $header.$content.$footer;
            });

            $show->field('status', '是否已派发')->using(\App\Models\TodoTask::$todo_status_options);
            $show->field('name.name', '创建者');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TodoTask(), function (Form $form) {
            $form->text('task_name')
                ->maxLength(30, '任务名称不能起过30个字符')
                ->required();
            $form->markdown('task_desc')
                ->options([
                    'mode' => 'markdown',
                    'imageUpload' => false,
                ]);
            $form->hidden('creator_uid')->value(Auth::guard('admin')->id());
            $form->footer(function ($footer) {
                // 去掉`重置`按钮
//                $footer->disableReset();
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
